package com.asen.libcommon.widget

import android.app.Activity
import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.view.View
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.annotation.ColorInt
import androidx.fragment.app.Fragment
import com.asen.libcommon.R
import com.asen.libcommon.databinding.ViewTitleToolbarBinding
import com.asen.libcommon.util.CommonUtil
import com.asen.libcommon.util.RxUtils.click
import org.jetbrains.anko.horizontalMargin

/**
 * @date   : 2021/8/30
 * @author : asenLiang
 * @e-mail : liangAisiSen@163.com
 * @desc   : 封装公用的标头控件
 */

class TitleToolbarView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : RelativeLayout(context, attrs, defStyleAttr) {

    private val mBinding: ViewTitleToolbarBinding

    init {
        val a = context.obtainStyledAttributes(attrs, R.styleable.TitleToolbarView)
        val title = a.getString(R.styleable.TitleToolbarView_toolbar_title_text)
        val size = a.getDimension(
            R.styleable.TitleToolbarView_toolbar_title_size,
            CommonUtil.sp2px(context, 17f).toFloat()
        )
        val color =
            a.getColor(R.styleable.TitleToolbarView_toolbar_title_color, Color.parseColor("#453d4d"))
        a.recycle()
        inflate(context, R.layout.view_title_toolbar, this)
        mBinding = ViewTitleToolbarBinding.bind(this)
        setTitle(title, CommonUtil.px2sp(context, size), color)
        setBackgroundColor(resources.getColor(R.color.white_FFFFFF))

        mBinding.apply {
            mIconLeft.click { mLeftCallback?.invoke(it) }
        }

    }

    private var mLeftCallback: ((view: View) -> Unit)? = null

    fun setLeftClick(leftCallback: ((view: View) -> Unit)? = null){
        mLeftCallback = leftCallback
    }

    fun setTitle(
        title: String?,
        size: Float? = null,
        @ColorInt color: Int? = Color.parseColor("#453d4d"),
        leftIcon: Int? = R.mipmap.icon_back,
        rightViewData: MutableList<View>? = null,
        leftCallback: ((view: View) -> Unit)? = null
    ) {
        mBinding.apply {
            mTitle.apply {
                if (!title.isNullOrEmpty()) text = title
                if (size != null) textSize = size
                if (color != null) setTextColor(color)
            }
            mIconLeft.apply {
                leftIcon?.let { setImageResource(leftIcon) }
                mLeftCallback = leftCallback
            }
            rightContainer.apply {
                removeAllViews()
                rightViewData?.forEachIndexed { index, it ->
                    addView(it)
                }
            }
        }
    }

    /** 添加图标 */
    fun addImageView(
        icon: Int,
        viewWidth: Int = LayoutParams.WRAP_CONTENT,
        viewHeight: Int = LayoutParams.WRAP_CONTENT,
        callback: (() -> Unit)? = null
    ): ImageView {
        return ImageView(context).apply {
            setBackgroundResource(icon)
            layoutParams = LayoutParams(viewWidth, viewHeight).apply {
                horizontalMargin = 10
            }
            callback?.let { click { callback.invoke() } }
        }
    }

    /** 添加文本 */
    fun addTextView(
        title: String,
        size: Float = 12f,
        color: Int? = null,
        viewWidth: Int = LayoutParams.WRAP_CONTENT,
        viewHeight: Int = LayoutParams.WRAP_CONTENT,
        callback: (() -> Unit)? = null
    ): TextView {
        return TextView(context).apply {
            text = title
            textSize = size
            if (color != null) setTextColor(color)
            layoutParams = LayoutParams(viewWidth, viewHeight).apply {
                horizontalMargin = 10
            }
            callback?.let { click { callback.invoke() } }
        }
    }

}


/**
 * 设置标题
 */
fun Activity.extITopToolbarTitle(
    view: TitleToolbarView,
    title: String? = null,
    size: Float? = null,
    @ColorInt color: Int = Color.parseColor("#453d4d"),
    leftIcon: Int? = R.mipmap.icon_back,
    rightViewData: MutableList<View>? = null,
    leftCallback: (() -> Unit)? = null
) {
    view.setTitle(title, size, color, leftIcon, rightViewData, leftCallback = {
        if (leftCallback != null) leftCallback.invoke() else {
            finish()
        }
    })
}


/**
 * 设置标题
 */
fun Fragment.extITopToolbarTitle(
    view: TitleToolbarView,
    title: String,
    size: Float? = 17f,
    @ColorInt color: Int? = Color.parseColor("#453d4d"),
    leftIcon: Int = R.mipmap.icon_back,
    rightViewData: MutableList<View>? = null,
    leftCallback: ((view: View) -> Unit)? = null
) {
    view.setTitle(title, size, color, leftIcon, rightViewData, leftCallback)
}